package ysq.xyg.util;

import com.github.pagehelper.StringUtil;
import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
/**
 * @author yu sheng qiang
 * @create 2017-03-27 16:17
 **/
public class BaseUtil {
    public static final String NUMBER_CONTENT="0123456789";
    public static final String LETTER_CONTENT="abcdefghijklmnopqrstuvwxyz";
    public static final String LETTER_NUMBER_CONTENT="0123456789abcdefghijklmnopqrstuvwxyz";
    public static final String UPPERCASE_LETTER_NUMBER_CONTENT="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };
    //获取文件的后缀
    public static String acceptFileType(String fileName){
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        return suffix;
    }
    //获取随机数
    public static String acceptRandom(String content,int num){
        StringBuffer sb=new StringBuffer();
        for (int i=0;i<num;i++){
            int id=(int)((Math.random())*(content.length()-1));
            sb.append(content.substring(id,id+1));
        }
        return sb.toString();
    }
    //获取id
    public static  String uId(){
        return new Date().getTime()+acceptRandom(NUMBER_CONTENT,8);
    }

    public static String urlEnodeUTF8(String str){
        String result = str;
        try {
            result = URLEncoder.encode(str,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取随机数
     * @param num
     * @param content
     * @return
     */
    public static String getRandom(int num,String content){
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<num;i++){
            int id=(int)(Math.random()*(content.length()-1));
            String code=content.substring(id,id+1);
            sb.append(code);
        }
        return sb.toString();
    }

    // 判断一个集合是否为空
    public static <T> boolean isEmpty(Collection<T> col) {
        if (col == null || col.isEmpty()) {
            return true;
        }

        return false;
    }

    // 判断一个集合是否不为空
    public static <T> boolean isNotEmpty(Collection<T> col) {
        return !isEmpty(col);
    }

    // 判断Map是否为空
    public static <K, V> boolean isEmpty(Map<K, V> map) {
        if (map == null || map.isEmpty()) {
            return true;
        }

        return false;
    }

    // 判断Map是否不为空为空
    public static <K, V> boolean isNotEmpty(Map<K, V> map) {
        return !isEmpty(map);
    }

    // 去除list中的重复数据
    public static <T> List<T> removeRepeat(List<T> list) {
        if (isEmpty(list)) {
            return list;
        }

        List<T> result = new ArrayList<T>();
        for (T e : list) {
            if (!result.contains(e)) {
                result.add(e);
            }
        }

        return result;
    }

    // 将集合转换为String数组
    public static <T> String[] toArray(List<T> list) {
        if (isEmpty(list)) {
            return null;
        }

        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = String.valueOf(list.get(i));
        }

        return result;
    }

    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }

    /**
     * Base64 解码
     * @param name
     * @return
     */
    public static String decodeBase64(String name){
        try {
            return new String(Base64.decodeBase64(name),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * Base64 编码
     * @param name
     * @return
     */
    public static String encodeBase64String(String name){
        try {
            return Base64.encodeBase64String(name.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void descartes(List<List<String>> dimvalue, List<List<String>> result, int layer, List<String> curList) {
        if (layer < dimvalue.size() - 1) {
            if (dimvalue.get(layer).size() == 0) {
                descartes(dimvalue, result, layer + 1, curList);
            } else {
                for (int i = 0; i < dimvalue.get(layer).size(); i++) {
                    List<String> list = new ArrayList<String>(curList);
                    list.add(dimvalue.get(layer).get(i));
                    descartes(dimvalue, result, layer + 1, list);
                }
            }
        } else if (layer == dimvalue.size() - 1) {
            if (dimvalue.get(layer).size() == 0) {
                result.add(curList);
            } else {
                for (int i = 0; i < dimvalue.get(layer).size(); i++) {
                    List<String> list = new ArrayList<String>(curList);
                    list.add(dimvalue.get(layer).get(i));
                    result.add(list);
                }
            }
        }
    }

    /***
     * double 添加
     * @param v1
     * @return
     */
    public static double addNormal(double... v1) {
        double res = 0;
        for (int i = 0; i < v1.length; i++) {
            res += v1[i];
        }
        return res;
    }

    public static strictfp double addDouble(double... v) {
        double res = 0;
        for (int i = 0; i < v.length; i++) {
            res += v[i];
        }
        return res;
    }

    /**
     *            double
     * @return double
     */
    public static double add(double... v) {
        BigDecimal b  = new BigDecimal(Double.toString(v[0]));
        for (int i = 1; i < v.length; i++) {
            BigDecimal b2 = new BigDecimal(Double.toString(v[i]));
            b=b.add(b2);
        }
        return b.doubleValue();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Double profit2=(40*66.67)/100.0;
        System.err.println(add(profit2));
        System.err.println("普通 "+ addNormal(1.2,0.6,0.8,1.4));
        System.err.println("strictfp "+addDouble(1.2,0.6,0.8,1.4));
        System.err.println("BigDEcimel: "+add(1.2,0.6,0.8,1.4));
        String name="     连帽长款羽绒服(帽子不可拆毛领可拆)【80%白鸭绒20%毛片/品蓝/市场价899】";
        int n=name.trim().length();
        String names=name.substring(0,16);
        if(StringUtil.isNotEmpty(name)&&name.length()>50){

        }

    }
}
